﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaiGaoQiaoWeb.Models;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.JsonEntities;
using System.Text;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class InPlanController : Controller
    {
        //
        // GET: /InPlan/
        private ArticleSelInfo invinfo = new ArticleSelInfo();
        private Work_ArticleUnit unit = new Work_ArticleUnit();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetInPlan(PageVM pvm) 
        {
            string articleName = string.Empty;
            string articleNo = string.Empty;
            string bringTime = string.Empty;
            StringBuilder query = new StringBuilder();
            if (string.IsNullOrEmpty(pvm.query))
            {
                query.Append(" ");
            }
            else
            {
                string[] param = pvm.query.Split(',');
            
                if (!string.IsNullOrEmpty(param[0]))
                {
                    query.AppendFormat(" AND ArticleName like '%{0}%'", param[0]);
                    articleName=param[0];
                }
                if (!string.IsNullOrEmpty(param[1]))
                {
                    query.AppendFormat(" AND ArticleNo ='{0}'", param[1]);
                    articleNo=param[1];
                }
                if (!string.IsNullOrEmpty(param[2]))
                {
                    query.AppendFormat(" AND BRINTTIME<='{0}'", param[2]);
                    bringTime= param[2];
                }
            }
            IList<ArticleSelInfo> invlist = invinfo.SelInventoryInfoSelInfo(pvm.rp, pvm.page, query.ToString());
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = invinfo.GetArticleInfoSelCount(query.ToString());
            List<string> v;
            foreach (ArticleSelInfo item in invlist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.ArticleNO.ToString());
                 v.Add(item.ArticleName);
                 v.Add(!string.IsNullOrEmpty(item.ArticleUnit) ? unit.GetCon_ArticleUnitById(Convert.ToInt32(item.ArticleUnit)).Unit : "");
                 v.Add(item.STOCKS_COUNT.ToString());
                 v.Add(RtnBringCount(bringTime,item.BRING_COUNT,item.ID,item.ArticleUnit).ToString());
                 #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ArticleNO.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }








        public int RtnBringCount(string dt,int bringcount,int id,string unit)
        {
            if (string.IsNullOrEmpty(dt)||Convert.ToDateTime(dt) <= DateTime.Now)
            {
                return bringcount;
            }
            else
            {
                return bringcount-invinfo.GetPlanCount(dt, id, unit);
                
            }
        
        }






    }
}
